/*
 * Copyright (c) 2016. For Intelligent Group.
 */

package com.intelligent.mts.da.client;

import com.intelligent.mts.da.handler.AdapterClientHandler;
import com.intelligent.mts.da.handler.HeartbeatClientHandler;
import com.intelligent.mts.da.handler.HeartbeatHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Julia.Zhou on 2016/07/20.
 */
public class MtsClient {
    public static Logger logger = LoggerFactory.getLogger(MtsClient.class);

    public void connect(String host, int port) throws Exception {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.AUTO_READ, true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(
                            new StringEncoder(),
                            new StringDecoder(),
                            new AdapterClientHandler());
                   // ch.pipeline().addLast(new IdleStateHandler(40, 0, 0), new HeartbeatClientHandler());


                }
            });
            ChannelFuture f = b.connect(host, port).sync();
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        MtsClient client = new MtsClient();
        client.connect("172.15.3.49", 6666);
    }
}
